﻿namespace AC.ExtendedRenderer.Toolkit
{
    using ComponentFactory.Krypton.Toolkit;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Text;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms;

    [ToolboxItem(true)]
    public class KryptonSliderButton : UserControl, IContentValues
    {
        private IPalette _palette;
        private PaletteRedirect _paletteRedirect;
        private IContainer components;
        internal Timer FireTimer;
        private KryptonManager k_manager = new KryptonManager();
        private ButtonStyles m_buttonstyle = ButtonStyles.MinusButton;
        private bool m_down;
        private bool m_highlight;
        private Color m_innerColor = Color.FromArgb(0x63, 0x6a, 0x74);
        private IDisposable m_mementoBack1;
        private IDisposable m_mementoBack2;
        private IDisposable m_mementoContent;
        private VisualOrientation m_orientation;
        private Color m_outerColor = Color.FromArgb(0xec, 0xec, 0xec);
        private PaletteBackInheritRedirect m_paletteBack;
        private PaletteBorderInheritRedirect m_paletteBorder;
        private PaletteContentInheritRedirect m_paletteContent;
        private bool m_singleClick;
        private PaletteBackStyle m_visuallook;

        public event SliderButtonFireEventHandler SliderButtonFire;

        public KryptonSliderButton()
        {
            this.InitializeComponent();
            base.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            base.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            base.SetStyle(ControlStyles.ResizeRedraw, true);
            base.SetStyle(ControlStyles.UserPaint, true);
            if (this._palette != null)
            {
                this._palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
            }
            KryptonManager.GlobalPaletteChanged += new System.EventHandler(this.OnGlobalPaletteChanged);
            this._palette = KryptonManager.CurrentGlobalPalette;
            this._paletteRedirect = new PaletteRedirect(this._palette);
            this.m_paletteBack = new PaletteBackInheritRedirect(this._paletteRedirect);
            this.m_paletteBorder = new PaletteBorderInheritRedirect(this._paletteRedirect);
            this.m_paletteContent = new PaletteContentInheritRedirect(this._paletteRedirect);
            this.BackColor = Color.Transparent;
            base.Size = new Size(0x10, 0x10);
            this.InitColors();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (this.m_mementoContent != null)
                {
                    this.m_mementoContent.Dispose();
                    this.m_mementoContent = null;
                }
                if (this.m_mementoBack1 != null)
                {
                    this.m_mementoBack1.Dispose();
                    this.m_mementoBack1 = null;
                }
                if (this.m_mementoBack2 != null)
                {
                    this.m_mementoBack2.Dispose();
                    this.m_mementoBack2 = null;
                }
                if (this.components != null)
                {
                    this.components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        private void DrawPlusInner(Graphics Gfx, Color Fill)
        {
            Gfx.FillRectangle(new SolidBrush(Fill), new Rectangle(4, (base.Height / 2) - 1, 8, 2));
            Gfx.FillRectangle(new SolidBrush(Fill), new Rectangle((base.Width / 2) - 1, 4, 2, 8));
        }

        private void DrawPlusOuter(Graphics Gfx, Color Fill)
        {
            Gfx.FillRectangle(new SolidBrush(Fill), new Rectangle(3, (base.Height / 2) - 2, 10, 4));
            Gfx.FillRectangle(new SolidBrush(Fill), new Rectangle((base.Width / 2) - 2, 3, 4, 10));
        }

        private void FireTimer_Tick(object sender, EventArgs e)
        {
            if (this.SliderButtonFire != null)
            {
                this.SliderButtonFire(this, new EventArgs());
            }
        }

        private GraphicsPath GetButtonPath(RectangleF Bounds)
        {
            GraphicsPath path = new GraphicsPath();
            path.AddEllipse(Bounds);
            return path;
        }

        private PaletteState GetButtonState()
        {
            if (!base.Enabled)
            {
                return PaletteState.Disabled;
            }
            if (this.m_down)
            {
                if (this.m_highlight)
                {
                    return PaletteState.CheckedPressed;
                }
                return PaletteState.Pressed;
            }
            if (this.m_highlight)
            {
                return PaletteState.Tracking;
            }
            return PaletteState.Normal;
        }

        public Image GetImage(PaletteState state)
        {
            return null;
        }

        public Color GetImageTransparentColor(PaletteState state)
        {
            return Color.Empty;
        }

        public string GetLongText()
        {
            return string.Empty;
        }

        public GraphicsPath GetRoundedSquarePath(RectangleF bounds)
        {
            GraphicsPath path = new GraphicsPath();
            path.AddEllipse(bounds);
            path.CloseFigure();
            return path;
        }

        public string GetShortText()
        {
            return string.Empty;
        }

        private void InitColors()
        {
            this.m_innerColor = this._palette.ColorTable.GripDark;
            this.m_outerColor = this._palette.ColorTable.GripLight;
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            this.FireTimer = new Timer(this.components);
            base.SuspendLayout();
            this.FireTimer.Interval = 0x3e8;
            this.FireTimer.Tick += new System.EventHandler(this.FireTimer_Tick);
            base.AutoScaleMode = AutoScaleMode.None;
            this.DoubleBuffered = true;
            this.Font = new Font("Segoe UI", 8.25f);
            base.Name = "KryptonSliderButton";
            base.Size = new Size(0x10, 0x10);
            base.MouseLeave += new System.EventHandler(this.KryptonSliderButton_MouseLeave);
            base.MouseDown += new MouseEventHandler(this.KryptonSliderButton_MouseDown);
            base.MouseUp += new MouseEventHandler(this.KryptonSliderButton_MouseUp);
            base.MouseEnter += new System.EventHandler(this.KryptonSliderButton_MouseEnter);
            base.ResumeLayout(false);
        }

        private void k_palette_BasePaletteChanged(object sender, EventArgs e)
        {
            base.Invalidate();
        }

        private void k_palette_BaseRendererChanged(object sender, EventArgs e)
        {
            base.Invalidate();
        }

        private void k_palette_PalettePaint(object sender, PaletteLayoutEventArgs e)
        {
            base.Invalidate();
        }

        private void KryptonSliderButton_MouseDown(object sender, MouseEventArgs e)
        {
            this.m_down = true;
            if (!this.m_singleClick)
            {
                this.FireTimer.Start();
            }
            base.Invalidate();
        }

        private void KryptonSliderButton_MouseEnter(object sender, EventArgs e)
        {
            this.m_highlight = true;
            base.Invalidate();
        }

        private void KryptonSliderButton_MouseLeave(object sender, EventArgs e)
        {
            this.m_highlight = false;
            base.Invalidate();
        }

        private void KryptonSliderButton_MouseUp(object sender, MouseEventArgs e)
        {
            this.m_down = false;
            if (!this.m_singleClick)
            {
                this.FireTimer.Stop();
            }
            else
            {
                this.SliderButtonFire(this, new EventArgs());
            }
            base.Invalidate();
        }

        private void OnGlobalPaletteChanged(object sender, EventArgs e)
        {
            if (this._palette != null)
            {
                this._palette.PalettePaint -= new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
            }
            this._palette = KryptonManager.CurrentGlobalPalette;
            this._paletteRedirect.Target = this._palette;
            if (this._palette != null)
            {
                this._palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
                this.InitColors();
            }
            base.Invalidate();
        }

        protected override void OnLayout(LayoutEventArgs e)
        {
            if (this._palette != null)
            {
                PaletteState buttonState = this.GetButtonState();
                Rectangle availableRect = new Rectangle(0, 0, 0x10, 0x10);
                IRenderer renderer = this._palette.GetRenderer();
                using (ViewLayoutContext context = new ViewLayoutContext(this, renderer))
                {
                    this.m_paletteContent.Style = (PaletteContentStyle) this.m_visuallook;
                    if (this.m_mementoContent != null)
                    {
                        this.m_mementoContent.Dispose();
                    }
                    this.m_mementoContent = renderer.RenderStandardContent.LayoutContent(context, availableRect, this.m_paletteContent, this, this.m_orientation, buttonState, false);
                }
            }
            base.OnLayout(e);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Rectangle clipRect = new Rectangle(0, 0, 0x10, 0x10);
            RectangleF bounds = new RectangleF(0f, 0f, 15.1f, 15.1f);
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            base.OnPaint(e);
            if (this._palette != null)
            {
                IRenderer renderer = this._palette.GetRenderer();
                using (RenderContext context = new RenderContext(this, e.Graphics, clipRect, renderer))
                {
                    this.m_paletteBack.Style = PaletteBackStyle.HeaderPrimary;
                    using (GraphicsPath path = this.GetButtonPath(clipRect))
                    {
                        this.m_mementoBack1 = renderer.RenderStandardBack.DrawBack(context, clipRect, path, this.m_paletteBack, this.m_orientation, base.Enabled ? PaletteState.Normal : PaletteState.Disabled, this.m_mementoBack1);
                    }
                    PaletteState buttonState = this.GetButtonState();
                    this.m_paletteBack.Style = this.m_visuallook;
                    this.m_paletteBorder.Style = (PaletteBorderStyle) this.m_visuallook;
                    this.m_paletteContent.Style = (PaletteContentStyle) this.m_visuallook;
                    if (this.m_paletteBack.GetBackDraw(buttonState) == InheritBool.True)
                    {
                        using (GraphicsPath path2 = this.GetRoundedSquarePath(bounds))
                        {
                            this.m_mementoBack2 = renderer.RenderStandardBack.DrawBack(context, clipRect, path2, this.m_paletteBack, this.m_orientation, buttonState, this.m_mementoBack2);
                        }
                    }
                    if (this.m_paletteBorder.GetBorderDraw(buttonState) == InheritBool.True)
                    {
                        e.Graphics.DrawEllipse(new Pen(this.m_paletteBorder.GetBorderColor2(buttonState)), bounds);
                    }
                    e.Graphics.SmoothingMode = SmoothingMode.None;
                    switch (this.m_buttonstyle)
                    {
                        case ButtonStyles.PlusButton:
                            this.DrawPlusOuter(e.Graphics, this.m_outerColor);
                            this.DrawPlusInner(e.Graphics, this.m_innerColor);
                            return;

                        case ButtonStyles.MinusButton:
                        {
                            Rectangle rect = new Rectangle(3, (base.Height / 2) - 2, 10, 4);
                            Rectangle rectangle3 = new Rectangle(4, (base.Height / 2) - 1, 8, 2);
                            e.Graphics.FillRectangle(new SolidBrush(this.m_outerColor), rect);
                            e.Graphics.FillRectangle(new SolidBrush(this.m_innerColor), rectangle3);
                            return;
                        }
                    }
                }
            }
        }

        private void OnPalettePaint(object sender, PaletteLayoutEventArgs e)
        {
            base.Invalidate();
        }

        public ButtonStyles ButtonStyle
        {
            get
            {
                return this.m_buttonstyle;
            }
            set
            {
                this.m_buttonstyle = value;
                base.Invalidate();
            }
        }

        public int EventFireRate
        {
            get
            {
                return this.FireTimer.Interval;
            }
            set
            {
                if ((this.FireTimer.Interval != value) & !this.m_down)
                {
                    this.FireTimer.Interval = value;
                }
            }
        }

        public VisualOrientation Orientation
        {
            get
            {
                return this.m_orientation;
            }
            set
            {
                if (this.m_orientation != value)
                {
                    this.m_orientation = value;
                    base.PerformLayout();
                    base.Invalidate();
                }
            }
        }

        public bool SingleClick
        {
            get
            {
                return this.m_singleClick;
            }
            set
            {
                this.m_singleClick = value;
                base.Invalidate();
            }
        }

        public PaletteBackStyle VisualLook
        {
            get
            {
                return this.m_visuallook;
            }
            set
            {
                if (this.m_visuallook != value)
                {
                    this.m_visuallook = value;
                    base.Invalidate();
                }
            }
        }

        public enum ButtonStyles
        {
            PlusButton,
            MinusButton
        }

        public delegate void SliderButtonFireEventHandler(KryptonSliderButton Sender, EventArgs e);
    }
}

